package ws.very.util.lang

import scala.util.Try

object MapExtractor extends ShortType with FnCookies{

  trait SeqE extends StringOptExtractor{
    def mStr(key: S) = { (map: Map[String, Seq[S]]) => map.get(key).map(_.headOption.map(_.trim).filter(_.length > 0)).flatten }.toCase
    def mSeq(key: S) = { (map: Map[String, Seq[S]]) => map.get(key) }.toCase
  }

  object SeqE extends SeqE

  trait StringE extends StringOptExtractor{
    def mStr(key: S) = { (map: Map[String, S]) => map.get(key).map(_.trim).filter(_.length>0)}.toCase
    def mStrOpt(key:S) ={ mStr(key).apply _ andThen(v=>Some(v))}.toCase
  }

  object StringE extends StringE

  trait StringOptExtractor {
    val mInt = { (strOpt: O[S]) =>strOpt.flatMap(str=>Try { str.toInt }.toOption)}.toCase
    val mLong = { (strOpt: O[S]) => strOpt.flatMap(str=>Try { str.toLong }.toOption)}.toCase
    val mBool = { (strOpt: O[S]) =>strOpt.flatMap(str=>Try { str.toBoolean }.toOption)}.toCase
    val mDouble = { (strOpt:O[S])=>strOpt.flatMap(str=>Try { str.toDouble }.toOption)}.toCase
  }
}
